package jp.melodroid.no4.list2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.view.View;
import android.app.ListActivity;

import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class list2 extends ListActivity
{

    private class MyListData extends HashMap<String,Object>
    {
	static final String IMAGE = "image";
	static final String TITLE = "title";
	static final String CONTENT = "content";

	private void init(int img,String text1,String text2) {
	    this.put(IMAGE,img);
	    this.put(TITLE,text1);
	    this.put(CONTENT,text2);
	    return;
	}

	public MyListData(int img,String text1,String text2) {
	    init(img,text1,text2);
	    return;
	}

	public MyListData(int img,int id1,int id2) {
	    String text1 = getResources().getString(id1);
	    String text2 = getResources().getString(id1);

	    init(img,text1,text2);
	    return;
	}
    }

    private ArrayList<MyListData> mkList() {
	ArrayList<MyListData> mList = new ArrayList<MyListData>();

	mList.add(new MyListData(R.drawable.f_b1_r,R.string.app_name,R.string.app_name));
	mList.add(new MyListData(R.drawable.f_b1_b,"1","きゃー"));
	mList.add(new MyListData(R.drawable.f_b1_g,"2","かけ"));
	mList.add(new MyListData(R.drawable.f_b1_r,"3","なび"));
	mList.add(new MyListData(R.drawable.f_b1_y,"4","さん"));
	mList.add(new MyListData(R.drawable.f_b1_d,"5","かっこ"));
	mList.add(new MyListData(R.drawable.f_b1_p,"6","いい"));
	mList.add(new MyListData(R.drawable.f_b1_s,"7","けっ"));
	mList.add(new MyListData(R.drawable.f_b1_b,"8","っっ"));
	mList.add(new MyListData(R.drawable.f_b1_b,"9","とう"));
	mList.add(new MyListData(R.drawable.f_b1_b,"10","して"));
	mList.add(new MyListData(R.drawable.f_b1_b,"11","くだ"));
	mList.add(new MyListData(R.drawable.f_b1_b,"12","さい"));
	return mList;
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

	ArrayList<MyListData> mList = mkList();
	String[] from = new String[] {MyListData.IMAGE,
				      MyListData.TITLE,
				      MyListData.CONTENT};
	int[] to = {R.id.imageview,R.id.text1,R.id.text2};

	SimpleAdapter mAdapter = new SimpleAdapter(this, mList,
						   R.layout.item_row,
						   from, to);
	setListAdapter(mAdapter);

	ListView lv = getListView();
	lv.setOnItemClickListener(new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
		    TextView tv = (TextView)view.findViewById(R.id.text2);
		    Toast ts = Toast.makeText(getApplicationContext(),
				        tv.getText(),Toast.LENGTH_SHORT);
		    ts.show();
		}
	    });
    }
}
